package com.longrise.android.byjk.plugins.vip.integralexchange;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;

import com.longrise.android.byjk.event.MineRefreshEvent;
import com.longrise.android.byjk.event.IntegralExchangeRefreshEvent;
import com.longrise.android.byjk.event.MyIntegralEvent;
import com.longrise.android.byjk.event.PersonalExchibitionEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalExhibitionActivity;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.common.datasource.remote.LoadDataManager;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2017/5/2.
 * 积分兑换点击图片 显示大图和预览界面
 */

public class IntegralExchangeImageActivity extends BaseActivity implements View.OnClickListener {

    public static final String IMAGEURL_INTENT = "imageurl_intent";
    public static final String LOCKSTATE_INTENT = "lockstate_intent"; //解锁状态  1:已解锁  0：未解锁
    public static final String NEEDSCORS_INTENT = "needscors_intent"; //需要的分数
    public static final String DETAILPICURL_INTENT = "detailpicurl_intent"; //预览图片
    public static final String GOODSID_INTENT = "goodsid_intent"; //goodsid
    public static final String GOODSTYPE_INTENT = "goodstype_intent"; //goodtype
    public static final String CURRENT_BACK_POSTION = "current_back_position";

    public static final int PAGE_FROM_PERSONALEX = 1; //来自展业秀更换背景
    public static final int PAGE_FROM_MYINTEGRAL = 2; //来自积分兑换
    public static final String PAGE_FROM_FLAG = "page_from_flag";

    private ImageView mIv;
    private TextView mBt;
    private String mImageUrl;
    private String mLockState;
    private TextView mTvright;
    private RelativeLayout mRlright;
    private ImageView mIvback;
    private ImageView mIv2;
    private String mNeedScore;
    private boolean mIsBigPic = true; //当前是不是展示的大图片 或者是预览图片
    private String mDetailpicurl;
    private String mGoodid;
    private String mGoodtype;
    private int mFrompage;



    private int mCurrentBackGroundPosition; //当前背景position

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_integralexchangeimage;
    }

    @Override
    public void initView() {
        mTvright = (TextView) findViewById(R.id.integral_exchange_image_right_tv);
        mRlright = (RelativeLayout) findViewById(R.id.integral_exchange_image_right_rl);
        mIvback = (ImageView) findViewById(R.id.integral_exchange_image_backicon);
        mIv = (ImageView) findViewById(R.id.integral_exchange_image_iv);
        mIv2 = (ImageView) findViewById(R.id.integral_exchange_image_iv2);
        mBt = (TextView) findViewById(R.id.integral_exchange_image_tv);
        getIntentData();
        initEvent();

        Tracker_Google.getInstance().addGoogleScreenName("展业秀背景" + mCurrentBackGroundPosition);
    }

    private void initEvent() {
        mIvback.setOnClickListener(this);
        mRlright.setOnClickListener(this);
        mBt.setOnClickListener(this);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        mImageUrl = intent.getStringExtra(IMAGEURL_INTENT);
        mLockState = intent.getStringExtra(LOCKSTATE_INTENT);
        mNeedScore = intent.getStringExtra(NEEDSCORS_INTENT);
        mDetailpicurl = intent.getStringExtra(DETAILPICURL_INTENT);
        mGoodid = intent.getStringExtra(GOODSID_INTENT);
        mGoodtype = intent.getStringExtra(GOODSTYPE_INTENT);
        mFrompage = intent.getIntExtra(PAGE_FROM_FLAG, PAGE_FROM_MYINTEGRAL);
        mCurrentBackGroundPosition = intent.getIntExtra(CURRENT_BACK_POSTION, 0);
        initData();
    }

    private void initData() {
        Glide.with(mContext).load(mImageUrl).into(mIv);
        Glide.with(mContext).load(mDetailpicurl).into(mIv2);
        showBtmBt(mLockState, mNeedScore);
        showRightTvandBg();
    }

    /**
     * 右边显示预览 或者 取消预览
     */
    private void showRightTvandBg() {
        if (mIsBigPic) {
            mTvright.setText("预览");
            mIv.setVisibility(View.VISIBLE);
            mIv2.setVisibility(View.GONE);
        } else {
            mTvright.setText("取消预览");
            mIv.setVisibility(View.GONE);
            mIv2.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 底部按钮文字
     *
     * @param lockState //解锁状态  1:已解锁  0：未解锁
     */
    private void showBtmBt(String lockState, String needScore) {
        switch (lockState) {
            case "0":
                mBt.setText("使用" + needScore + "积分兑换");
                mBt.setClickable(true);
                break;
            case "1":
                mBt.setText("立即使用");
                mBt.setClickable(true);
                break;
            case "2":
                mBt.setText("使用中");
                mBt.setClickable(false);
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.integral_exchange_image_backicon: //返回按钮
                finish();
                break;
            case R.id.integral_exchange_image_right_rl: //预览 或者 取消预览
                mIsBigPic = !mIsBigPic;
                showRightTvandBg();
                addEvent("预览");
                break;
            case R.id.integral_exchange_image_tv: //底部按钮
                toBtmClick();
                break;
        }
    }

    /**
     * 底部按钮点击
     */
    public void toBtmClick() {
        if ("1".equals(mLockState)) {
            toChangeSkin(mGoodid);
            addEvent("立即使用");
        } else if ("0".equals(mLockState)) {
            checkIsEnoughIntegral(mGoodid,mGoodtype,mNeedScore);
            addEvent("积分兑换");
        }
    }

    /**
     * 换肤
     */
    private void toChangeSkin(String goodsid) {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("skinid", goodsid);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        showLoadingDialog();
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_vip_uVIPstateBySkinid", fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        if (mFrompage == PAGE_FROM_PERSONALEX) { //展业秀刷新数据
                            EventBus.getDefault().post(new PersonalExchibitionEvent(PersonalExchibitionEvent.PECategory.GHPF));
                            EventBus.getDefault().post(new IntegralExchangeRefreshEvent(true)); //我的积分刷新数据
                        } else {
                            EventBus.getDefault().post(new IntegralExchangeRefreshEvent(false)); //我的积分刷新数据
                            startActivity(PersonalExhibitionActivity.class);
                        }
                        finish();
                    } else {
                        showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

            }
        });
    }


    /**
     * 检查积分是否充足
     */
    private void checkIsEnoughIntegral(final String goodsid, final String goodstype, final String needscores) {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("goodsid", goodsid);
        bean.set("goodstype", goodstype);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        showLoadingDialog();
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_isEnoughCostgoods", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int resultstate = (Integer) bean.get("resultstate");
                            if (resultstate == 1) { //积分充足
                                IntegralExchangeDialogHelper.toShowIntegralExchangeDialog(IntegralExchangeImageActivity.this,
                                        goodsid, goodstype, needscores,
                                        new IntegralExchangeDialogHelper.ToIntegralExchangeListener() {
                                            @Override
                                            public void toIntegralExchange(String goodsid, String goodstype) {
                                                togetIntegralExchange(goodsid, goodstype);
                                            }
                                        });
                            } else if (resultstate == 2) { //积分不足
                                IntegralExchangeDialogHelper.showIntegralnotenoughtDialog(IntegralExchangeImageActivity.this);
                            } else {
                                showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                    }
                });
    }


    /**
     * 积分兑换
     */
    private void togetIntegralExchange(String goodsid, String goodstype) {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("goodsid", goodsid);
        bean.set("goodstype", goodstype);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        showLoadingDialog();
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_vip_iScoresCostRecord", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            String resultdesc = bean.getString("resultdesc");
                            int value = (Integer) bean.get("resultstate");
                            if (value == 1) { //积分兑换成功
                                EntityBean resultbean = bean.getBean("result");
                                mLockState = "1";
                                showBtmBt(mLockState, mNeedScore);
                                EventBus.getDefault().post(new IntegralExchangeRefreshEvent(false));
                                EventBus.getDefault().post(new MyIntegralEvent(true));
                                MineRefreshEvent mineRefreshEvent = new MineRefreshEvent();
                                mineRefreshEvent.setBBye(true);
                                EventBus.getDefault().post(mineRefreshEvent); //我的页面保宝余额与我的积分是同一个接口
                                showToast(resultdesc);
                            } else if (value == 2) {
                                IntegralExchangeDialogHelper.showIntegralnotenoughtDialog(IntegralExchangeImageActivity.this);
                            } else {
                                showToast(resultdesc);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

            @Override
            public void onFinished(String key, String service) {
                dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

            }
        });
    }

    private void addEvent(String action){
        Tracker_Google.getInstance().addEvent("展业秀背景" + mCurrentBackGroundPosition, action, "");
    }
}
